const { dialog } = require("electron");
const nodeXlsx = require("node-xlsx");
const utils = require("./utils");

const cmd = {
    selectPath: function (arg) {
        return new Promise((resolve, reject) => {
            dialog
                .showOpenDialog(arg["window"], {
                    filters: arg["filter"],
                    properties: [arg["prop"], "multiSelections"],
                })
                .then((res) => {
                    resolve(res.filePaths);
                });
        });
    },
    query: function (arg) {
        let mainWindow = arg["window"];

        let dirname = arg["path"];
        let state = arg["state"];
        let negation = arg["negation"];
        let qureyText = arg["text"];
        let filesList = [];
        let pathKeys = arg["pathKeys"];
        if (dirname instanceof Array) {
            filesList = dirname;
        } else {
            //判断是否为单文件
            if (dirname.indexOf(".xlsx") > -1 || dirname.indexOf(".xls") > -1) {
                filesList.push(dirname);
            } else {
                filesList = utils.readFileList(
                    dirname,
                    [".xlsx", ".xls"],
                    pathKeys,
                    [],
                    state,
                    negation
                );
            }
        }
        let maxlen = 0;
        let promieList = [];
        for (let pathIndex = 0; pathIndex < filesList.length; pathIndex++) {
            let sheets = nodeXlsx.parse(filesList[pathIndex]);
            for (let z = 0; z < sheets.length; z++) {
                promieList.push(
                    new Promise((resolve, reject) => {
                        let rowsData = [];
                        // sheet.data是所有行数据
                        let rows = sheets[z].data;

                        for (let i = 0; i < rows.length; i++) {
                            let data = {};
                            let flag = false;
                            for (let j = 0; j < rows[i].length; j++) {
                                let name = "attr_" + j;
                                data[name] = rows[i][j];
                                if (!data[name]) {
                                    data[name] = "";
                                }
                                let queryArr = qureyText.split(",");
                                for (
                                    let qIndex = 0;
                                    qIndex < queryArr.length;
                                    qIndex++
                                ) {
                                    if (flag) {
                                        break;
                                    }
                                    if (
                                        data[name]
                                            .toString()
                                            .indexOf(queryArr[qIndex]) > -1
                                    ) {
                                        if (rows[i].length > maxlen) {
                                            maxlen = rows[i].length;
                                        }
                                        flag = true;
                                        break;
                                    }
                                }
                            }
                            data["path"] = filesList[pathIndex];
                            if (flag) {
                                rowsData.push(data);
                            }
                        }
                        let result = {
                            maxlen: maxlen,
                            rowsData: rowsData,
                        };
                        mainWindow.webContents.send("main", result);
                        resolve("ok");
                    })
                );
            }

            Promise.all(promieList).then((res) => {
                let result = {
                    isOver: 1,
                };
                mainWindow.webContents.send("main", result);
            });
        }
    },
};

exports.cmd = cmd;
